#import "LocationAnnotationView.h"
#import "Define.h"
#import <QuartzCore/QuartzCore.h>
#import "MainPageViewController.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"

@implementation LocationAnnotationView

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier andDelegate:(id)delegate;
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self != nil)
    {
        _delegate = delegate;
        CGRect frame = self.frame;
        frame.size = CGSizeMake(WIDTH_TABLE_CELL, HEIGHT_TABLE_CELL);
        self.frame = frame;
        self.backgroundColor = [UIColor clearColor];
        self.centerOffset = CGPointMake(-WIDTH_TABLE_CELL/2, -HEIGHT_TABLE_CELL/2);
        [self initInterfaceFromAnotation:annotation];
    }
    return self;
}

- (void)initInterfaceFromAnotation:(id <MKAnnotation>)annotation;
{
    // INTERFACE
    _imageViewAvatar = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width - WIDTH_TABLE_CELL + 3, 3, WIDTH_TABLE_CELL - 6, HEIGHT_TABLE_CELL - 6)];
    _imageViewAvatar.layer.borderColor = [UIColor whiteColor].CGColor;
    _imageViewAvatar.layer.borderWidth = 3.0f;
    _imageViewAvatar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    [self addSubview:_imageViewAvatar];
    
    _imageViewAvailability = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width - WIDTH_TABLE_CELL/2, 0, HEIGHT_TABLE_CELL/2, WIDTH_TABLE_CELL/2)];
    _imageViewAvailability.image = IMG_AVAILABILITY;
    _imageViewAvailability.hidden = YES;
    [self addSubview:_imageViewAvailability];
    
    _btnRemainingDay = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - WIDTH_TABLE_CELL/2 - 3, -5, HEIGHT_TABLE_CELL/2, WIDTH_TABLE_CELL/2 - 10)];
    _btnRemainingDay.titleLabel.font = [UIFont boldSystemFontOfSize:5];
    [_btnRemainingDay setBackgroundImage:IMG_REMAINING_DAY forState:UIControlStateNormal];
    _btnRemainingDay.hidden = YES;
    [self addSubview:_btnRemainingDay];
    
    _imageViewAvatar.layer.masksToBounds = YES;
    _imageViewAvatar.layer.cornerRadius = INT_RADIUS_MEDIUM_AVATAR-2;
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = INT_RADIUS_MEDIUM_AVATAR;
    self.backgroundColor = [UIColor clearColor];
    
    // GESTURE
    UIView *viewTouchedGesture = [[UIView alloc] initWithFrame:_imageViewAvatar.frame];
    [self addSubview:viewTouchedGesture];
    TouchedGestureRecognizer *touchedGestureRecognizer = [TouchedGestureRecognizer new];
    touchedGestureRecognizer.delegateCallBack = self;
    [viewTouchedGesture addGestureRecognizer:touchedGestureRecognizer];
}

- (void)loadInterfaceFromAnotation:(id<MKAnnotation>)annotation;
{
    PersonNodeData *roomNode = (PersonNodeData *)annotation;
    
    // AVATAR
    [_imageViewAvatar setImageWithURL:[NSURL URLWithString:roomNode.personSmallAvatarURL] placeholderImage:IMG_AVARTAR_DUMMY];
    
    // STATUS
    if (roomNode.personPhongDepStatus.intValue > 0) {
        _imageViewAvailability.hidden = YES;
        _btnRemainingDay.hidden = NO;
        [_btnRemainingDay setTitle:STRING_REMAINING_DAY_FORMAT(roomNode.personPhongDepStatus.intValue) forState:UIControlStateNormal];
    }
    else {
        _imageViewAvailability.hidden = NO;
        _btnRemainingDay.hidden = YES;
    }
}

- (void)didSelectAnnotationView; {
    self.backgroundColor = [UIColor blueColor];
}

- (void)didDeselectAnnotationView; {
    self.backgroundColor = [UIColor clearColor];
}

#pragma mark - TouchedGestureRecognizerProtocol

- (void)touchesOneClickCallback:(NSSet *)touches withEvent:(UIEvent *)event {
//    NSLog(@"touchesOneClickCallback");
    if ([_delegate respondsToSelector:@selector(touchesOneClickCallbackWithAnnotation:)]) {
        [_delegate touchesOneClickCallbackWithAnnotation:self.annotation];
    }
}

- (void)touchesDoubleClickCallback:(NSSet *)touches withEvent:(UIEvent *)event {
//    NSLog(@"touchesDoubleClickCallback");
    if ([_delegate respondsToSelector:@selector(touchesDoubleClickCallbackWithAnnotation:)]) {
        [_delegate touchesDoubleClickCallbackWithAnnotation:self.annotation];
    }
}

@end
